function preload_images(images)
 {
  var img = new Array();
  for(var i = 0; i < images.length; ++i)
   {
    img[i] = new Image();
    img[i].src = images[i];
   }
 }

function sf(id)
 {
  if(document.getElementById(id)) document.getElementById(id).focus();
 }

function simple_ajax_request(url,query)
 {
  var strURL = 'index.php';
  var xmlHttpReq = false;
  var self = this;
  // Mozilla/Safari
  if (window.XMLHttpRequest) {
      self.xmlHttpReq = new XMLHttpRequest();
  }
  // IE
  else if (window.ActiveXObject) {
      self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  }
  self.xmlHttpReq.open('POST', strURL, true);
  self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  self.xmlHttpReq.setRequestHeader("Pragma", "no-cache");
  self.xmlHttpReq.setRequestHeader("Cache-Control", "must-revalidate");
  self.xmlHttpReq.setRequestHeader("If-Modified-Since", document.lastModified);
  self.xmlHttpReq.onreadystatechange = function() {
      if(self.xmlHttpReq.readyState == 4) {
       // just do nothing...   
      }
  }
  self.xmlHttpReq.send(query);
 }


function highlight_posting(thread,threadcolor,posting,postingcolor)
 {
  if(document.getElementById(thread)) document.getElementById(thread).style.background=threadcolor;
  if(document.getElementById(posting)) document.getElementById(posting).style.background=postingcolor;
 }

function fold_thread(id,expand_img,fold_img,expand_title,fold_title)
 {
  var ULs = document.getElementById('thread-'+id).getElementsByTagName("ul"); 
  if(ULs[0] && (ULs[0].style.display=='block' || ULs[0].style.display==''))
   {
    document.getElementById('expand_img_'+id).src = expand_img;
    document.getElementById('expand_link_'+id).title = expand_title;
    for(var i = 0; i < ULs.length; i++) 
    ULs[i].style.display = 'none'; 
   }
  else
   {
    document.getElementById('expand_img_'+id).src = fold_img;
    document.getElementById('expand_link_'+id).title = fold_title;
    for(var i = 0; i < ULs.length; i++) 
    ULs[i].style.display = 'block'; 
   } 
 }

function hide_posting(id,show_posting_img,hide_posting_img)
 {
  if(document.getElementById('posting-'+id).style.display!='none') 
   {
    document.getElementById('posting-'+id).style.display='none';
    if(document.getElementById('avatar-'+id)) document.getElementById('avatar-'+id).style.display='none';
    if(document.getElementById('hide-posting-'+id)) document.getElementById('hide-posting-'+id).src=show_posting_img;
   } 
  else
   {
    document.getElementById('posting-'+id).style.display='block';
    if(document.getElementById('avatar-'+id)) document.getElementById('avatar-'+id).style.display='block';
    if(document.getElementById('hide-posting-'+id)) document.getElementById('hide-posting-'+id).src=hide_posting_img;
   } 
 } 

function getElementsByClassName(class_name)
{
  var all_obj,ret_obj=new Array(),j=0;
  if(document.all)all_obj=document.all;
  else if(document.getElementsByTagName && !document.all)all_obj=document.getElementsByTagName("*");
  for(i=0;i<all_obj.length;i++)
  {
    if(all_obj[i].className==class_name)
    {
      ret_obj[j]=all_obj[i];
      j++
    }
  }
  return ret_obj;
}

function hide_all_postings(show_posting_image)
 {
  var obj=getElementsByClassName('postingcontainer');
  for(i=0;i<obj.length;i++)
   {
    obj[i].style.display='none';
   }
  var obj=getElementsByClassName('hide-posting');
  for(i=0;i<obj.length;i++)
   {
    obj[i].src=show_posting_image;
   }
  var obj=getElementsByClassName('avatar');
  for(i=0;i<obj.length;i++)
   {
    obj[i].style.display='none';
   }
 }
 
function show_all_postings(hide_posting_image)
 {
  var obj=getElementsByClassName('postingcontainer');
  for(i=0;i<obj.length;i++)
   {
    obj[i].style.display='block';
   }
  var obj=getElementsByClassName('hide-posting');
  for(i=0;i<obj.length;i++)
   {
    obj[i].src=hide_posting_image;
   }
  var obj=getElementsByClassName('avatar');
  for(i=0;i<obj.length;i++)
   {
    obj[i].style.display='block';
   }   
 } 
 
function hide_replies()
 {
  document.getElementsByTagName("ul").style.display='none';
 }   

function hide_sidebar(id,minimized_width,maximized_width,hide_image,show_image)
 {
  if(document.getElementById(id+'-container').style.display=='none') 
   {
    document.getElementById(id+'-container').style.display='block';
    document.getElementById(id+'-toggle').src=hide_image;
    document.getElementById(id).style.width=maximized_width;
    simple_ajax_request('index.php','hide='+id);
   } 
  else
   {
    document.getElementById(id+'-container').style.display='none';
    document.getElementById(id+'-toggle').src=show_image;
    document.getElementById(id).style.width=minimized_width;
    simple_ajax_request('index.php','hide='+id);
   } 
 }
 
function toggle_sidebar(hide_image,show_image)
 {
  //this.blur();
  if(document.getElementById('sidebarcontent').style.display=='none') 
   {
    document.getElementById('sidebarcontent').style.display='block';
    document.getElementById('sidebartoggle').src=hide_image;
   } 
  else
   {
    document.getElementById('sidebarcontent').style.display='none';
    document.getElementById('sidebartoggle').src=show_image;
   } 
  simple_ajax_request('index.php','toggle_sidebar=true');
 } 

function bbcode(form,field,code,value)
 {
  value = typeof(value) != 'undefined' ? value : '';
  if(value!='') value = '='+value;
  
  if (document.selection) // for IE
   {
    var str = document.selection.createRange().text;
    document.forms[form].elements[field].focus();
    var sel = document.selection.createRange();
    sel.text = '[' + code + value + "]" + str + '[/' + code + ']';
    return;
   }
  else if((typeof document.forms[form].elements[field].selectionStart) != 'undefined') // for Mozilla
   {
    var txtarea = document.forms[form].elements[field];
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    var oldScrollTop = txtarea.scrollTop;
    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd);
    var s3 = (txtarea.value).substring(selEnd, selLength);
    txtarea.value = s1 + '[' + code + value + ']' + s2 + '[/' + code + ']' + s3;
    txtarea.selectionStart = s1.length;
    txtarea.selectionEnd = s1.length + code.length*2 + value.length + s2.length + 5;
    txtarea.scrollTop = oldScrollTop;
    txtarea.focus();
    return;
   }
  else insert('[' + code + value + '][/' + code + ']');
 }

function insert(form,field,what)
 {
  if (document.forms[form].elements[field].createTextRange)
   {
    document.forms[form].elements[field].focus();
    document.selection.createRange().duplicate().text = what;
   }
  else if ((typeof document.forms[form].elements[field].selectionStart) != 'undefined') // for Mozilla
   {
    var tarea = document.forms[form].elements[field];
    var selEnd = tarea.selectionEnd;
    var txtLen = tarea.value.length;
    var txtbefore = tarea.value.substring(0,selEnd);
    var txtafter =  tarea.value.substring(selEnd, txtLen);
    var oldScrollTop = tarea.scrollTop;
    tarea.value = txtbefore + what + txtafter;
    tarea.selectionStart = txtbefore.length + what.length;
    tarea.selectionEnd = txtbefore.length + what.length;
    tarea.scrollTop = oldScrollTop;
    tarea.focus();
   }
  else
   {
    document.forms[form].elements[field].value += what;
    document.forms[form].elements[field].focus();
   }
 }

function insert_link(form,field,link_text,link_target)
 {
 if (document.selection) // for IE
   {
    var str = document.selection.createRange().text;
    document.forms[form].elements[field].focus();
    var sel = document.selection.createRange();
    var insert_link = prompt(link_target,'http://');
    if(sel.text=='' && insert_link!='' && insert_link!=null) str = prompt(link_text,'');

    if(insert_link && str!=null)
     {
      if(str!='')
       {
        sel.text = "[link=" + insert_link + "]" + str + "[/link]";
       }
      else
       {
        sel.text = "[link]" + insert_link + "[/link]";
       }
     }
    return;
   }
  else if ((typeof document.forms[form].elements[field].selectionStart) != 'undefined') // for Mozilla
   {
    var txtarea = document.forms[form].elements[field];
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    var oldScrollTop = txtarea.scrollTop;
    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd);
    var s3 = (txtarea.value).substring(selEnd, selLength);

    var insert_link = prompt(link_target,'http://');
    if(selEnd-selStart==0 && insert_link!='' && insert_link!=null) s2 = prompt(link_text,'');
    if(insert_link && s2!=null)
     {
      if(s2!='')
       {
        txtarea.value = s1 + '[link=' + insert_link + ']' + s2 + '[/link]' + s3;
        var codelength = 14 + insert_link.length + s2.length;
       }
      else
       {
        txtarea.value = s1 + '[link]' + insert_link + '[/link]' + s3;
        var codelength = 13 + insert_link.length;
       }
      txtarea.selectionStart = s1.length;
      txtarea.selectionEnd = s1.length + codelength;
      txtarea.scrollTop = oldScrollTop;
      txtarea.focus();
      return;
     }
   }
  else insert('[link=http://www.domain.tld/]Link[/link]');
 }

function clear_input(form,field)
 {
  document.forms[form].elements[field].value = '';
  document.forms[form].elements[field].focus();
 }

function show_box(id,x,y)
 {
  x = typeof(x) != 'undefined' ? x : 0;
  y = typeof(y) != 'undefined' ? y : 0;
  if(!document.getElementById(id).style.display || document.getElementById(id).style.display=='none')
   {
    s_box = document.getElementById(id);
    s_box.style.display = 'block';
    s_box.style.left  = xpos+x + 'px';
    s_box.style.top = ypos+y + 'px';
   } 
  else
   {
    document.getElementById(id).style.display = 'none';
   }
 }

function popup(url,width,height)
 {
  width = typeof(width) != 'undefined' ? width : 340;
  height = typeof(height) != 'undefined' ? height : 340;
  winpops = window.open(url,'','width='+width+',height='+height+',scrollbars,resizable');
 }

function delete_cookie(deleting_cookie_message) 
 {
  document.getElementById('delete_cookie').innerHTML = decodeURIComponent(deleting_cookie_message);
    
    var strURL = 'index.php';
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.setRequestHeader("Pragma", "no-cache");
    self.xmlHttpReq.setRequestHeader("Cache-Control", "must-revalidate");
    self.xmlHttpReq.setRequestHeader("If-Modified-Since", document.lastModified);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            //var deleted = self.xmlHttpReq.responseXML.getElementsByTagName('deleted')[0].firstChild.data;
            document.getElementById('delete_cookie').innerHTML = '&nbsp;';
            document.getElementById('setcookie').checked = '';
        }
    }
    //self.xmlHttpReq.send(true);
    self.xmlHttpReq.send('mode=delete_cookie&method=ajax');
 }

function ajax_preview(id) {
    //this.blur();
    if(document.getElementById("ajax-preview").className == "active-"+id && document.getElementById("ajax-preview").style.display=='none') document.getElementById("ajax-preview").className = "";
    if(document.getElementById("ajax-preview").className == "active-"+id) 
     {
      document.getElementById("ajax-preview").className = ""; 
      hide_element('ajax-preview');
     } 
    else
    { 
    document.getElementById("ajax-preview").className = 'active-'+id;
    
    document.getElementById('ajax-preview');
    document.getElementById("ajax-preview-content").innerHTML = '';
    if(document.getElementById("ajax-preview-replylink")) document.getElementById("ajax-preview-replylink").style.display = 'none';
    
    ap = document.getElementById('ajax-preview');
    ap.style.display = "block";
    ap.style.left  = xpos-8 + "px";
    ap.style.top = ypos+2 + "px";
        
    // show throbber:
    document.getElementById("ajax-preview-throbber").style.display = "block";
    
    var strURL = 'index.php';
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.setRequestHeader("Pragma", "no-cache");
    self.xmlHttpReq.setRequestHeader("Cache-Control", "must-revalidate");
    self.xmlHttpReq.setRequestHeader("If-Modified-Since", document.lastModified);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(id,self.xmlHttpReq.responseXML);
        }
    }
    //self.xmlHttpReq.send(true);
    self.xmlHttpReq.send('mode=entry&ajax_preview=true&id='+id);
    }
}

function ajax_thread(id) {
	var at_id='';
	at_id='atf_'+id;
    if(document.getElementById(at_id).className == "active-"+id && document.getElementById(at_id).style.display=='none') document.getElementById(at_id).className = "";
    if(document.getElementById(at_id).className == "active-"+id) 
     {
      document.getElementById(at_id).className = ""; 
      hide_element(at_id);
      //document.getElementById(at_id).innerHTML='';
	  //document.getElementById(at_id).style.display='none';
     } 
    else
    { 
    document.getElementById(at_id).className = 'active-'+id;
    
    document.getElementById(at_id);    
    document.getElementById('atc_'+id).innerHTML='';
    if(document.getElementById('areply_'+id)) document.getElementById('areply_'+id).style.display = 'none';
    
    ap = document.getElementById(at_id);
    ap.style.display = "block";
    //ap.style.left  = xpos + "px";
    //ap.style.top = ypos+2 + "px";
        
    document.getElementById('atc_'+id).innerHTML ='Loading...';
    var strURL = 'index.php';
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.setRequestHeader("Pragma", "no-cache");
    self.xmlHttpReq.setRequestHeader("Cache-Control", "must-revalidate");
    self.xmlHttpReq.setRequestHeader("If-Modified-Since", document.lastModified);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updateajaxpage(id,self.xmlHttpReq.responseXML);
        }
    }
    //self.xmlHttpReq.send(true);
    self.xmlHttpReq.send('mode=entry&ajax_preview=true&id='+id);
    }
}

function updatepage(id,xml){
    
    //var subject = xml.getElementsByTagName('subject')[0].firstChild.data;
    var content = xml.getElementsByTagName('content')[0].firstChild.data;
    if(content=='') content = '<p>-</p>';
    document.getElementById("ajax-preview-content").innerHTML = content;
    // hide throbber:
    document.getElementById("ajax-preview-throbber").style.display = "none";
    if(document.getElementById("ajax-preview-replylink")) 
     {
      document.getElementById("ajax-preview-replylink").style.display = 'block';
      //document.getElementById("ajax-preview-replylink").innerHTML = '[ <a href="index.php?mode=posting&id='+id+'">'+reply_link+'</a> ]';
      document.getElementById("replylink").href = 'index.php?mode=posting&id='+id;
     } 
}

function updateajaxpage(id,xml){
    
    var content = xml.getElementsByTagName('content')[0].firstChild.data;
    if(content=='') content = '<p>-</p>';
    document.getElementById('atc_'+id).innerHTML = content;
    if(document.getElementById('areply_'+id)) 
     {
      document.getElementById('areply_'+id).style.display = 'block';
      //document.getElementById("ajax-preview-replylink").innerHTML = '[ <a href="index.php?mode=posting&id='+id+'">'+reply_link+'</a> ]';
      document.getElementById('areply_'+id).href = 'index.php?mode=posting&id='+id;
     } 
}

function mouse_position(e) 
 {
  if (!e) e = window.event;
  xpos  = e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  ypos = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
 }

function hide_element(e) 
 {
  document.getElementById(e).style.display = 'none';
 }

function mark(id,marked_image,unmarked_image,process_mark_image,mark_title,unmark_title) 
 {
    //this.blur();
    
    pmi = new Image();
    pmi.src = process_mark_image;
    mi = new Image();
    mi.src = marked_image;
    umi = new Image();
    umi.src = unmarked_image;
        
    document.getElementById('markimg_'+id).src = process_mark_image;
    document.getElementById('markimg_'+id).alt = '[ ]';
    
    var strURL = 'index.php';
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.setRequestHeader("Pragma", "no-cache");
    self.xmlHttpReq.setRequestHeader("Cache-Control", "must-revalidate");
    self.xmlHttpReq.setRequestHeader("If-Modified-Since", document.lastModified);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            var action = self.xmlHttpReq.responseXML.getElementsByTagName('action')[0].firstChild.data;
            if(action==1) 
             {
              document.getElementById('markimg_'+id).src = marked_image;
              document.getElementById('markimg_'+id).alt = '[●]';
              document.getElementById('marklink_'+id).title = decodeURIComponent(unmark_title);
              document.getElementById('markimg_'+id).title = decodeURIComponent(unmark_title);
             } 
            else 
             {
              document.getElementById('markimg_'+id).src = unmarked_image;
              document.getElementById('markimg_'+id).alt = '[○]';
              document.getElementById('marklink_'+id).title = decodeURIComponent(mark_title);
              document.getElementById('markimg_'+id).title = decodeURIComponent(mark_title);
             } 
        }
    }
    //self.xmlHttpReq.send(true);
    self.xmlHttpReq.send('mode=posting&mark='+id+'&method=ajax');
 }

function delete_posting_confirm(this_link,confirm_question)
 {
  var confirmed = confirm(decodeURIComponent(confirm_question));
  if(confirmed) this_link.href += '&delete_posting_confirm=true';
  return confirmed;
 } 
function drain_posting_confirm(this_link,confirm_question)
 {
  var confirmed = confirm(decodeURIComponent(confirm_question));
  if(confirmed) this_link.href += '&drain_posting_confirm=true';
  return confirmed;
 } 
function undrain_posting_confirm(this_link,confirm_question)
 {
  var confirmed = confirm(decodeURIComponent(confirm_question));
  if(confirmed) this_link.href += '&drain_posting_confirm=true';
  return confirmed;
 } 

function unfold_posting_confirm(this_link,confirm_question)
 {
  var confirmed = confirm(decodeURIComponent(confirm_question));
  if(confirmed) this_link.href += '&fold_posting_confirm=true';
  return confirmed;
 } 
 
function fold_posting_confirm(this_link,confirm_question)
 {
	var comment = prompt(decodeURIComponent(confirm_question),'');
	if(comment != null){
		this_link.href += '&fold_posting_confirm=true&comment=' + comment;
		return true;
	}
	else{
		return false;
	}
 }

function clear_input(form,field)
 {
  document.forms[form].elements[field].value = '';
  document.forms[form].elements[field].focus();
 }

function hide_quote()
 {
  document.getElementById('insert_quote_link').style.visibility = 'visible';
  quotes = document.forms['postingform'].elements['text'].value;
  document.forms['postingform'].elements['text'].value = '';
 }
 
function insert_quote()
 {
  document.getElementById('insert_quote_link').style.visibility = 'hidden';
  var current_value = document.forms['postingform'].elements['text'].value;
  document.forms['postingform'].elements['text'].value = quotes + '\n\n' + current_value;
  document.forms['postingform'].elements['text'].focus();
 } 

function is_postingform_complete(name_error,subject_error,text_error,terms_of_use_error)
 {
	terms_of_use_error = typeof(terms_of_use_error) != 'undefined' ? terms_of_use_error : '';
  error_message='';
	if(document.forms['postingform'].elements['name'] && document.forms['postingform'].elements['name'].value=='')
	 {
		error_message += "- "+decodeURIComponent(name_error)+"\n";
	 }
	if(document.forms['postingform'].elements['subject'].value=='')
	 {
		error_message += "- "+decodeURIComponent(subject_error)+"\n";
	 }
	if(text_error!='' && document.forms['postingform'].elements['text'].value=='')
	 {
		error_message += "- "+decodeURIComponent(text_error)+"\n";
	 }
	if(terms_of_use_error!='' && document.forms['postingform'].elements['terms_of_use_agree'].checked==false)
	 {
		error_message += "- "+decodeURIComponent(terms_of_use_error)+"\n";
	 }
  if(error_message)
	 {
		alert(error_message);
		return false;
	 }
	else
	 {
		document.getElementById('throbber-submit').style.visibility = 'visible';
    return true;
	 }
 }

function insert_avatar(avatar)
 {
  document.getElementById('avatar').innerHTML = '<a href="index.php?mode=avatar" onclick="popup(\'index.php?mode=avatar\'); return false"><img src="'+avatar+'" alt="Avatar" /></a>';
 }

document.onmousedown = mouse_position;
